import axios from 'axios'
import { message } from 'antd'

export async function downFileCommon(url, data, type, token, filename) {
  axios({
    method: type,
    url,
    data,
    responseType: 'blob',
    headers: {
      Authorization: token,
    },
  })
    .then((res) => {
      console.log(res)
      // let type = res.headers['content-type'] //请求头中文件类型
      const blob = new Blob([res.data])
      // console.log('下载第一步==', blob, res)
      if ('download' in document.createElement('a')) {
        // 非IE下载
        const elink = document.createElement('a')
        elink.download = filename
        elink.style.display = 'none'
        elink.href = URL.createObjectURL(blob)
        document.body.appendChild(elink)
        elink.click()
        URL.revokeObjectURL(elink.href) // 释放URL 对象
        document.body.removeChild(elink)
        // console.log('下载第二步')
      } else {
        // IE10+下载
        navigator.msSaveBlob(blob, filename)
      }
    })
    .catch((err) => {
      message.error('下载失败')
      console.log('下载错误', err)
    })
}
